package com.hgkj.cloud.base.scheme.table;

import com.hgkj.cloud.base.scheme.ClsAnswerRequest;
import com.hgkj.cloud.base.scheme.ClsAnswerResult;
import com.hgkj.cloud.base.scheme.ClsScore;

import java.math.BigDecimal;
import java.util.Map;

/**
 * 十七、抑郁自评量表（Sds）共20题
 */
public class Table17 extends AbstractTable implements TableCalculate{
    String code = "Sds";

    @Override
    public String getCode() {
        return code;
    }

    @Override
    public ClsAnswerResult calc(ClsAnswerRequest answer) {
        return GetSdsFactorScore(answer);
    }

    @Override
    public String[] report(Map map) {
        return null;
    }

    private static ClsAnswerResult GetSdsFactorScore(ClsAnswerRequest answer) {
        // 1总粗分，2标准总分
        ClsAnswerResult result = new ClsAnswerResult();
        result.setMessage("");
        result.setType(answer.getType());
        result.setUuid(answer.getUuid());
        try {
            // 1正确题数
            int MAX_NUM = 20;

            int intI;
            String[] strAnswer_c = answer.getAnswer().split(g_sChar);
            if (strAnswer_c.length > 1) {
                for (intI = 1; intI < strAnswer_c.length; intI++) {
                    strAnswer_c[intI] = strAnswer_c[intI].trim();
                }
            }

            String[] sAnswer = strAnswer_c[0].toString().split(g_nChar);

            if (sAnswer.length != MAX_NUM) {
                result.setMessage("答案数量不符！");
                return result;
            }

            int i, j;

            // 共2组，第一组1,2,3,4，第二组4,3,2,1
            int[][] intNumberA = { { 1, 3, 4, 7, 8, 9, 10, 13, 15, 19 }, { 2, 5, 6, 11, 12, 14, 16, 17, 18, 20 } };

            float[][] vFactorA = new float[1][2]; // 实际为1个因子
            for (i = 0; i < vFactorA.length; i++) {
                for (j = 0; j < vFactorA[0].length; j++) {
                    vFactorA[i][j] = 0;
                }
            }

            // 原始分
            // 总粗分
            for (i = 0; i < intNumberA.length; i++) {
                if (i == 0) {
                    for (j = 0; j < intNumberA[1].length; j++) {
                        vFactorA[0][0] += Integer.valueOf(sAnswer[intNumberA[i][j] - 1]);
                    }
                } else {
                    for (j = 0; j < intNumberA[1].length; j++) {
                        vFactorA[0][0] += 5 - Integer.valueOf(sAnswer[intNumberA[i][j] - 1]);
                    }
                }
            }
            // 标准总分
            // 以下是计算并保留两位小数
            BigDecimal bd = new BigDecimal(vFactorA[0][0] * 1.25);
            vFactorA[0][1] = bd.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();

            // 按原始分“诊断结果”
            // 按原始分“指导意见”
            int[] intValueA = { 39, 47, 55 };
            String[] strValueA = { "无", "有（轻度）", "有（中度）", "有（重度）" };
            String vAppraise = strValueA[strValueA.length - 1] + "抑郁症状";
            String vDoctorIdea = "Sds.1.4";
            for (i = 0; i < intValueA.length; i++) {
                if (vFactorA[0][0] <= intValueA[i]) {
                    vAppraise = strValueA[i] + "抑郁症状";
                    vDoctorIdea = "Sds.1." + String.valueOf(i + 1);
                    break;
                }
            }

            ClsScore.Sds sds = new ClsScore.Sds();

            sds.setTotal_o(vFactorA[0][0]);
            sds.setTotal_s(vFactorA[0][1]);
            sds.setTotal_a(vAppraise);
            sds.setTotal_d(vDoctorIdea);

            result.setObject(sds);
            return result;
        } catch (ArithmeticException e) {
            result.setMessage(e.toString());
            return result;
        }
    }

}
